import fs from 'fs';
import path from "path";
import ExtractTextPlugin from "extract-text-webpack-plugin";

const postCssLoader = {
	loader: 'postcss-loader',
	options: {
		config: {
			path: path.join(__dirname, './postcss.config.js')
		}
	}
};
const jsx = {
	test: /\.(js|jsx)$/,
	exclude: /(node_modules|bower_components)/,
	// include: [resolve('app'),resolve('platforms')],
	// use:['react-hot-loader','babel-loader']
	loader: "babel-loader",
	query: {
		babelrc: false,
		presets: [
			["es2015", {
				"modules": false
			}],
			"stage-0", "react"
		],
		plugins: ["transform-decorators-legacy","react-hot-loader/babel", ["babel-plugin-root-import", [{
				"rootPathPrefix": "@",
				"rootPathSuffix": "src"
			}]],
			"transform-runtime",
			 ['import', {
				"libraryName": "antd",
				"style": "css"
			}]
		]
	}
};
const assets = {
	test: /\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif|mp4|webm)(\?\S*)?$/,
	loader: 'url-loader?limit=8192&name=images/[hash].[name].[ext]'
};

const html = {
	test: /\.html$/,
	exclude: /node_modules/,
	use: ['html-loader']
};

const cssLoaders = (options) => {
	options = options || {};
	const cssLoader = {
		loader: 'css-loader',
		options: {
			minimize: process.env.NODE_ENV === 'production',
			sourceMap: options.sourceMap
		}
	};
	const generateLoaders = (loader, loaderOptions) => {
		let loaders = [cssLoader];
		if (loader) {
			loaders.push({
				loader: loader + '-loader',
				options: Object.assign({}, loaderOptions, {
					sourceMap: options.sourceMap
				})
			});
		}
		if (options.extract) {
			return ExtractTextPlugin.extract({
				use: loaders,
				fallback: 'style-loader'
			});
		} else {
			return ['style-loader'].concat(loaders);
		}
	};
	return {
		css: generateLoaders(),
		postcss: generateLoaders(),
		less: generateLoaders('less'),
		// sass: generateLoaders('sass', {
		// 	indentedSyntax: true
		// }),
		// scss: generateLoaders('sass')
	};
};
const styleLoaders = (options) => {
	let output = [];
	let loaders = cssLoaders(options);
	for (let extension in loaders) {
		let loader = loaders[extension];
		output.push({
			test: new RegExp('\\.' + extension + '$'),
			use: loader
		});
	}
	return output;
};
export const getLoaders = (env) => {
	let output = env === 'production' ? styleLoaders({
		sourceMap: true,
		extract: true
	}) : styleLoaders({
		sourceMap: false,
		extract: false
	});
	output.push(html);
	output.push(jsx);
	output.push(assets);
	return output;
};
export default getLoaders;
